package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentTypeRelDao;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentTypeUserDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentTypeRel;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentTypeCount;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentTypeUser;

public class AgentTypeRelService {

    static Logger log = Logger.getLogger(AgentTypeRelService.class);

    public static AgentTypeRelService self() {
        return MultiDBTools.getService(AgentTypeRelService.class, true);
    }

    public static AgentTypeRelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentTypeRelService.class, true);
    }

    AgentTypeRelDao dao;
    AgentTypeUserDao userDao;

    public AgentTypeRelService(String jdbcKey) {
        this.dao = new AgentTypeRelDao(jdbcKey);
        this.userDao = new AgentTypeUserDao(jdbcKey);
    }

    public List<AgentTypeRel> findJoinByUserId(Long userId){
        if(userId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("userId",userId);
            try {
                return dao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public List<AgentTypeUser> findJoinByTypeId(Long typeId){
        if(typeId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("typeId",typeId);
            try {
                return userDao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<AgentTypeRel> findJoinByUserIds(List<Long> userIds){
    	if(userIds != null && userIds.size() > 0) {
    		Conditions condition = new Conditions(AgentTypeRel.class);
    		condition.and().in("userId", userIds);
    		
    		try {
				return dao.findJoinByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<Long> findTypeIdsByUserId(Long userId){
    	if(userId != null ) {
	    	Conditions conditions = new Conditions(AgentTypeRel.class);
	    	conditions.and().is("userId",userId);
	    	try {
				return dao.findTypeIds(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public AgentTypeRel getJoinByKeys(Long userId, Long typeId){
        if(userId != null && typeId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("userId",userId);
            conditions.and().is("typeId",typeId);
            try {
                return dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<AgentTypeCount> findCountByTypeIds(List<Long> typeIds){
    	if(typeIds != null && typeIds.size() > 0) {
	        Conditions conditions = new Conditions(AgentTypeRel.class);
	        conditions.and().in("typeId",typeIds);
	        try {
	            return dao.countGroupTypeId(conditions);
	        } catch (SQLException e) {
	            log.error(e.getMessage(), e);
	        }
    	}
        return null;
    }

    public boolean add(AgentTypeRel agentTypeRel) throws LogicException {
    	agentTypeRel.setCreateDate(new Date());
        try {
            return dao.insert(agentTypeRel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteByKeys(Long userId,Long typeId) throws LogicException{
        if(userId != null && typeId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("typeId", typeId);
            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByTypeId(Long typeId){
        if(typeId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("typeId", typeId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
    
    public boolean deleteByUserId(Long userId){
        if(userId != null) {
            Conditions conditions = new Conditions(AgentTypeRel.class);
            conditions.and().is("userId", userId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
